package com.solidtrust.app.controller;

import java.util.List;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solidtrust.entity.Area;
import com.solidtrust.entity.Brand;
import com.solidtrust.entity.Build;
import com.solidtrust.entity.Model;
import com.solidtrust.entity.Volume;
import com.solidtrust.repository.AreaRepository;
import com.solidtrust.repository.BrandRepository;
import com.solidtrust.repository.BuildRepository;
import com.solidtrust.repository.ModelRepository;


@RestController
@Transactional(readOnly = true)
@RequestMapping("public/api")
public class PublicAPIController {

	private static final Logger log4j = Logger.getLogger(PublicAPIController.class);
	@Autowired AreaRepository areaResp;
	@Autowired BrandRepository brandRep;
	@Autowired ModelRepository modelRep;
	@Autowired BuildRepository buildRep;
	
	@ExceptionHandler(Exception.class)
    public String exceptionHandler(Exception ex){    
       log4j.error(ex.getMessage(), ex);
       return "error";
    }
	
	@RequestMapping("areas")
	public List<Area> getProvinceAndCities(){
		return areaResp.getProvinceAndCities();
	}
	
	@RequestMapping("brands")
	public List<Brand> getBrands(){
		return brandRep.findByStatus((short)1);
	}
	
	
	@RequestMapping("models")
	public List<Model> getModels(@RequestParam("brand") Integer brandId){
		List<Model> list = brandRep.findOne(brandId).getModels();
		list.size();
		return list;
	}
	
	
	@RequestMapping("builds")
	public List<Build> getBuilds(@RequestParam("model") Integer modelId){
		List<Build> list = modelRep.findOne(modelId).getBuilds();
		list.size();
		return list;
	}
	
	
	@RequestMapping("volumes")
	public List<Volume> getVolumes(@RequestParam("build") Integer buildId){
		List<Volume> list = buildRep.findOne(buildId).getVolumes();
		list.size();
		return list;
	}
	
	
	
}
